import time
from websocket import create_connection

# url = 'wss://i.cg.net/wi/ws'
url = 'wss://ws.hotbit.io/'
while True:  # 一直链接，直到连接上就退出循环
    time.sleep(2)
    try:
        ws = create_connection(url)
        print(ws)
        break
    except Exception as e:
        print('连接异常：', e)
        continue
while True:  # 连接上，退出第一个循环之后，此循环用于一直获取数据
    # ws.send('{"event":"subscribe", "channel":"btc_usdt.ticker"}')
    ws.send('{"id": 104, "method": "server.ping", "params": []}')
    # ws.send('{"Origin": "https://www.hotbit.io", "Pragma": "no-cache", '
    #         '"Sec-WebSocket-Extensions": "permessage-deflate; client_max_window_bits", '
    #         '"Sec-WebSocket-Key": "iEm4/Lcr6ErLBgRVB7/RxA==", '
    #         '"Sec-WebSocket-Protocol": "chat", "Sec-WebSocket-Version": 13, '
    #         '"Upgrade": "websocket", '
    #         '"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36"}')
    response = ws.recv()
    print(response)
